package ttc.sftp.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

public class AsyncTaskDownload extends AsyncTask<ArrayList<String>, Integer, String> {
	int dataSize;
	ProgressBar progressBar;
	
	
	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
	}



	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		   myProgress = 0;

	}

	@Override
	protected String doInBackground(ArrayList<String>... params) {
		// TODO Auto-generated method stub
		ArrayList<String> tempArrayList = params[0];
		String username = tempArrayList.get(0);
		String password = tempArrayList.get(1);
		String host = tempArrayList.get(2);
		String stringPort= tempArrayList.get(3);
		String filePath=tempArrayList.get(4);
		String fileName= tempArrayList.get(5);
		
		Log.i("filen", filePath);
		
		int port = Integer.parseInt(stringPort);
		
		ChannelSftp sftpChannel;
		
		
		JSch jsch = new JSch();
        Session session = null;
        try {
            session = jsch.getSession(username, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect(); 

            Channel channel = session.openChannel("sftp");
            channel.connect();
            sftpChannel = (ChannelSftp) channel;
            try {
             	File sdCard = Environment.getExternalStorageDirectory();
            	File dir = new File(sdCard.getAbsolutePath());
            	File file = new File(dir,fileName);
            	FileOutputStream f = null;
				try {
					f = new FileOutputStream(file);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

            	
    			
            	sftpChannel.get(filePath, f);
            	
            	OutputStreamWriter myOutWriter = new OutputStreamWriter(f);
    			myOutWriter.close();
    			f.close();
            
            	
            	
			
            	  
			
			} catch (SftpException e) { 
				// TODO Auto-generated catch block
				e.printStackTrace();			
				sftpChannel.exit();
				session.disconnect(); 
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        } catch (JSchException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.            
        }
		
		
		return null;
	}

	
	int myProgress;
	@Override
	protected void onProgressUpdate(Integer... values) {
		// TODO Auto-generated method stub
		
		
		progressBar.incrementProgressBy(values[0]);
        
		super.onProgressUpdate(values);
	}

}
